package gw.com.android.ui.home;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import gw.com.android.presenter.http.FlashRequest;
import gw.com.android.ui.news.FlashAdapter;
import www.com.library.interfaces.ReqCallBack;
import www.com.library.view.XRecyclerView;

/**
 * @author jett
 * @since 2018-11-09.
 */
class HomeFlashPresenter {

    private LinearLayout llEmpty;
    private XRecyclerView rvList;
    private ProgressBar pbLoading;

    private final Activity mActivity;
    private boolean inited = false;
    private boolean loadSuccess = false;
    private FlashAdapter mAdapter;
    private FlashRequest request = new FlashRequest();

    HomeFlashPresenter(Activity activity, View flashView) {
        this.mActivity = activity;
        llEmpty = flashView.findViewById(R.id.flash_empty);
        rvList = flashView.findViewById(R.id.flash_list);
        pbLoading = flashView.findViewById(R.id.flash_loading);
        rvList.setRefreshFrozen(true);
        rvList.setScrollFrozen(true);
    }

    public void init(boolean frozen) {
        inited = true;
        //rvList.setScrollFrozen(frozen);
        rvList.setLayoutManager(new LinearLayoutManager(mActivity));
        rvList.hideFooter();
        rvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                requestNews();
            }

            @Override
            public void onLoadMore() {
                nextPage();
            }
        });
        List<FlashRequest.FlashData> list = request.fetchLocal();
        refreshUi(list, true);
        rvList.setRefreshing(true);
    }

    public void onSelected(boolean frozen) {
        if (!inited) {
            init(frozen);
        }
        if (!loadSuccess) {
            rvList.setRefreshing(true);
        }
    }

    public void onRefresh() {
        if (inited) {
            rvList.smoothScrollToPosition(0);
            rvList.setRefreshing(true);
        }
    }

    public void setFrozen(boolean frozen) {
        rvList.setScrollFrozen(frozen);
    }

    private void requestNews() {
        request.call(new ReqCallBack<List<FlashRequest.FlashData>>() {
            @Override
            public void onReqSuccess(List<FlashRequest.FlashData> result) {
                rvList.refreshComplete();
                refreshUi(result, false);
            }

            @Override
            public void onReqFailed(String errorMsg) {
                rvList.refreshComplete();
                refreshUi(null, false);
            }
        });
    }

    private void nextPage() {
        request.nextPage(new ReqCallBack<List<FlashRequest.FlashData>>() {
            @Override
            public void onReqSuccess(List<FlashRequest.FlashData> result) {
                rvList.loadMoreComplete();
                rvList.setNoMore(!request.hasNextPage());
                appendUi(result);
            }

            @Override
            public void onReqFailed(String errorMsg) {
                rvList.loadMoreComplete();
            }
        });
    }

    private void refreshUi(List<FlashRequest.FlashData> list, boolean isCache) {
        if (list == null || list.isEmpty()) {
            if (!loadSuccess && !isCache) {
                rvList.setVisibility(View.GONE);
                pbLoading.setVisibility(View.GONE);
                llEmpty.setVisibility(View.VISIBLE);
            }
            return;
        }
        if (!loadSuccess) {
            loadSuccess = true;
            llEmpty.setVisibility(View.GONE);
            pbLoading.setVisibility(View.GONE);
            rvList.setVisibility(View.VISIBLE);
        }
        if (mAdapter == null) {
            mAdapter = new FlashAdapter(mActivity);
            rvList.setAdapter(mAdapter);
        }
        mAdapter.updateData(list);
        rvList.setNoMore(!request.hasNextPage());
    }

    private void appendUi(List<FlashRequest.FlashData> list) {
        if (mAdapter == null || list == null || list.isEmpty()) {
            return;
        }
        mAdapter.appendData(list);
    }

}
